package site.xuxingcan.tvlauncher;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends Activity {

    private View browser;
    private View wireless;
    private int times = 0;
    private long lastTime = -1;
    private ImageView background_img;

    @Override
    public void onBackPressed() {
        return;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         /*set it to be no title*/
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        /*set it to be full screen*/
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        browser = findViewById(R.id.browser);
        background_img = (ImageView) findViewById(R.id.background_img);
        wireless = findViewById(R.id.wireless);
        browser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, BrowserActivity.class));
            }
        });
        wireless.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (System.currentTimeMillis() - lastTime < 5000) {
                    times++;
                } else {
                    times = 0;
                }
                lastTime = System.currentTimeMillis();
                if (times >= 10) {
                    lastTime = -1;
                    final EditText myEditText = new EditText(MainActivity.this);
                    new AlertDialog.Builder(MainActivity.this).setTitle("请输入管理员密码")
                            .setView(myEditText)
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    String s = myEditText.getText().toString();
                                    if ("1111".equals(s)) {
                                        MainActivity.this.finish();
//                                        startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
                                    } else {
                                        Toast.makeText(MainActivity.this, "管理员密码错误！", Toast.LENGTH_LONG).show();
                                    }
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            })
                            .setCancelable(false)
                            .show();
                } else {
                    startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
                }
            }
        });
        browser.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                ViewGroup.LayoutParams lp = browser.getLayoutParams();
                if (hasFocus) {
                    lp.height = getResources().getDimensionPixelSize(R.dimen.icon_wight_focus);
                    lp.width = getResources().getDimensionPixelSize(R.dimen.icon_wight_focus);
                } else {
                    lp.height = getResources().getDimensionPixelSize(R.dimen.icon_wight);
                    lp.width = getResources().getDimensionPixelSize(R.dimen.icon_wight);
                }
                browser.setLayoutParams(lp);
            }
        });
        wireless.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                ViewGroup.LayoutParams lp = wireless.getLayoutParams();
                if (hasFocus) {
                    lp.height = getResources().getDimensionPixelSize(R.dimen.icon_wight_focus);
                    lp.width = getResources().getDimensionPixelSize(R.dimen.icon_wight_focus);
                } else {
                    lp.height = getResources().getDimensionPixelSize(R.dimen.icon_wight);
                    lp.width = getResources().getDimensionPixelSize(R.dimen.icon_wight);
                }
                wireless.setLayoutParams(lp);
            }
        });
        browser.requestFocus();
        File file = new File(getDir("pic", Context.MODE_PRIVATE), "startup.jpg");
        //若该文件存在
        if (file.exists()) {
            try {
                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                if (bitmap != null) {
                    background_img.setImageBitmap(bitmap);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } catch (Error error) {
                error.printStackTrace();
            }
        }
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    HttpUtil.getImageURI(MainActivity.this);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 5 * 1000);
    }
}
